package com.it.park.controller.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.it.park.client.service.ActionService;
import com.it.park.common.JsonObj;
import com.it.park.common.Page;
import com.it.park.constant.Constant;
import com.it.park.model.Action;

@Controller
@RequestMapping("/admin/action")
public class AdminActionController {
	private String rootPath=Constant.ADMIN_PATH;
	
	private Page page = null;
	@Autowired
	private ActionService actionService;
	
	@RequestMapping("listRows")
	@ResponseBody
	public Map<String,Object> listRows(HttpServletRequest request){

		int pageNow = Integer.parseInt(request.getParameter("page"));
		int rows = Integer.parseInt(request.getParameter("rows"));
		Map<String,Object> map_ = new HashMap<String, Object>();
		Integer total = actionService.selectTotal(map_);
		if(page != null){
			page = new Page(pageNow,total,rows);
		}else{
			page = new Page(1,total,rows);
		}
		map_.put("startPos", page.getStartPos());
		map_.put("pageSize", page.getPageSize());
		List<Action> list = actionService.selectPagination(map_);
		Map<String,Object> map  =new HashMap<String, Object>();
		map.put("total", total);
		map.put("rows", list);
		return map;
	}
	
	@RequestMapping("actionList")
	public String actionList(HttpServletRequest request){
			return rootPath+"action/actionList";
	}
	
	@RequestMapping("save")
	@ResponseBody
	public JsonObj<Action> save(Action action,HttpServletRequest request){
		JsonObj<Action> json = new JsonObj<Action>();
		Action action2= actionService.selectByUrl(action.getActionUrl());
		boolean success = true;
		if(action2 != null){
			json.setMessage("该地址已经存在了!");
			success = false;
		}else{
			success = actionService.insertSelective(action);
			if(success){
				json.setMessage("添加成功!");
			}else{
				json.setMessage("添加失败!");
			}
		}
		json.setSuccess(success);
		json.setObject(action);
		return json;
	}
	
	@RequestMapping("deleteById")
	@ResponseBody
	public JsonObj<String> deleteById(@RequestParam("id") int id,HttpServletRequest request){
		JsonObj<String> json = new JsonObj<String>();
		boolean success = actionService.deleteByPrimaryKey(id);
		if(success){
			json.setMessage("删除成功!");
			json.setSuccess(true);
		}else{
			json.setMessage("删除失败!");
			json.setSuccess(false);
		}
		return json;
	}
	
	@RequestMapping("edit")
	@ResponseBody
	public JsonObj<String> edit(Action action,HttpServletRequest request){
		JsonObj<String> json = new JsonObj<String>();
		boolean success = actionService.updateByPrimaryKeySelective(action);
		if(success){
			json.setMessage("修改成功!");
			json.setSuccess(true);
		}else{
			json.setMessage("修改失败!");
			json.setSuccess(false);
		}
		return json;
	}
}
